package com.pf.service.impl;

import com.pf.mapper.ImproveMapper;
import com.pf.mapper.entity.ImproveEntity;
import com.pf.service.ImproveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author PanFei
 * @version 1.0.0
 * @createTime 2023/4/18 22:29
 */
@Service
public class ImproveServiceImpl implements ImproveService {

    @Autowired
    private ImproveMapper improveMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsert() {
        List<ImproveEntity> improveList = new ArrayList<>();
        for (int i = 0; i < 2000; i++) {
            ImproveEntity improve = ImproveEntity.builder()
                    .improveCode("code" + i)
                    .checkItem("checkItem" + i)
                    .issueDesc("issueDesc" + i)
                    .planFinishedDate(new Date())
                    .mainPerson("mainPerson" + i)
                    .build();
            improveList.add(improve);
        }

        long startTime = System.currentTimeMillis();
        improveMapper.batchInsert(improveList);
        long endTime = System.currentTimeMillis();
        System.out.println("耗时=" + (endTime - startTime) + "ms");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdate() {
        List<ImproveEntity> improveList = improveMapper.selectAllList();
        List<ImproveEntity> targetList = new ArrayList<>();
        for (int i = 0; i < improveList.size(); i++) {
            ImproveEntity improve = improveList.get(i);
            improve.setCheckItem("checkItem1" + i);
            improve.setIssueDesc("issueDesc1" + i);
            improve.setMainPerson("mainPerson1" + i);
            improve.setPlanFinishedDate(new Date());
            targetList.add(improve);
        }

        long startTime = System.currentTimeMillis();
        improveMapper.batchUpdate(targetList);
        long endTime = System.currentTimeMillis();
        // 728ms
        System.out.println("batchUpdate耗时=" + (endTime - startTime) + "ms");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdateWithCaseWhen() {
        List<ImproveEntity> improveList = improveMapper.selectAllList();
        List<ImproveEntity> targetList = new ArrayList<>();
        for (int i = 0; i < improveList.size(); i++) {
            ImproveEntity improve = improveList.get(i);
            improve.setCheckItem("checkItem2" + i);
            improve.setIssueDesc("issueDesc2" + i);
            improve.setMainPerson("mainPerson2" + i);
            improve.setPlanFinishedDate(new Date());
            targetList.add(improve);
        }

        long startTime = System.currentTimeMillis();
        improveMapper.batchUpdateWithCaseWhen(targetList);
        long endTime = System.currentTimeMillis();
        // 296ms
        System.out.println("batchUpdateWithCaseWhen耗时=" + (endTime - startTime) + "ms");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsertOrUpdate() {
        List<ImproveEntity> improveList = improveMapper.selectAllList();
        List<ImproveEntity> targetList = new ArrayList<>();
        for (int i = 0; i < improveList.size(); i++) {
            ImproveEntity improve = improveList.get(i);
            improve.setCheckItem("checkItem3" + i);
            improve.setIssueDesc("issueDesc3" + i);
            improve.setMainPerson("mainPerson3" + i);
            improve.setPlanFinishedDate(new Date());
            targetList.add(improve);
        }

        long startTime = System.currentTimeMillis();
        improveMapper.batchInsertOrUpdate(targetList);
        long endTime = System.currentTimeMillis();
        // 296ms
        System.out.println("batchInsertOrUpdate耗时=" + (endTime - startTime) + "ms");
    }
}
